﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ColdSun.Commons;
using ColdSun.Commons.Configs;
using ColdSun.Commons.Entities;
using ColdSun.Services;
using System.IO;
using ColdSun.Framework;

namespace ColdSun.BankManagements
{
    public partial class CtrlInsuranceForBranchByMonth : UserControl
    {
        private FrmProgressBar frmProgressBar = new FrmProgressBar();
        private Insurance selectInsurance = new Insurance();
        private string infoFile;
        public CtrlInsuranceForBranchByMonth()
        {
            InitializeComponent();
        }

        private void btnInsuranceExport_Click(object sender, EventArgs e)
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundInsuranceWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundInsuranceWorker_RunWorkerCompleted);

            // Kick off the Async thread
            backgroundWorker.RunWorkerAsync();

            // Lock up the UI with this modal progress form.
            frmProgressBar.ShowDialog();
        }

        private void BackgroundInsuranceWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Logger.Info(string.Format("----- BEGIN TO PROCESS INSURANCE BY MONTH-----"));
            try
            {
                //List<Replacement> replacementInfos = Utilities.GetReplacementInfo(this.infoFile);
                //if (replacementInfos == null)
                //{
                //    Logger.Info(string.Format("Can not get replacement information"));
                //    return;
                //}


                InsuranceService service = new InsuranceService();
                string result = service.GenerateInsuranceByBranchForMonth(
                    txtOriginalFile.Text, txtWeekSheetName.Text, txtBranchSheetName.Text,
                    this.selectInsurance, txtExportFile.Text, txtExportFolder.Text);
            }
            catch (Exception ex)
            {
                Logger.Error("Background worker - Do work", ex);
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally
            {
                Logger.Info(string.Format("----- END TO PROCESS INSURANCE BY MONTH-----"));
            }
        }

        private void BackgroundInsuranceWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (frmProgressBar != null)
            {
                frmProgressBar.Close();
            }
        }

        private void CtrlInsurance_Load(object sender, EventArgs e)
        {
            //Insurance
            if (File.Exists(Utilities.GetFullFileName(CommonConfigs.InsuranceForBranchByMonthOriginalFile)))
            {
                txtOriginalFile.Text = Utilities.GetFullFileName(CommonConfigs.InsuranceForBranchByMonthOriginalFile);
            }

            txtExportFile.Text = CommonConfigs.InsuranceForBranchByMonthExportFile;
            if (Directory.Exists(Utilities.GetFullFileName(CommonConfigs.InsuranceForBranchByMonthExportFolder)))
            {
                txtExportFolder.Text = Utilities.GetFullFileName(CommonConfigs.InsuranceForBranchByMonthExportFolder);
            }
            cmbInsuranceType.SelectedIndex = 0;
        }

        private void cmbInsuranceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectInsurance.Type = cmbInsuranceType.SelectedIndex;
            selectInsurance.Name = cmbInsuranceType.SelectedItem.ToString();
        }

        private void btnOriginalFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOriginalFile.Text = openFileDialog.FileName;
            }
        }

        private void btnExportFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtExportFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
